package com.clouddot.baipai.util;

/**
 * Created by wxy on 2018/4/2.
 */


import android.util.Log;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.List;

public class fileUtils {


    public static String createIfNotExist(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return path;
    }


    public static boolean writeBytes(String filePath, byte[] data) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(data);
            fos.close();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }


    public static byte[] readBytes(String file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            int len = fis.available();
            byte[] buffer = new byte[len];
            fis.read(buffer);
            fis.close();
            return buffer;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;

    }

    public static void writeString(String file, String content) {
        try {
            FileWriter writer = new FileWriter(file, true);
            writer.write(content);
            writer.write("\r\n");
            writer.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

//    public static void writeList(String file, List<CodeInfo> content, String[] strings) {
//        try {
//            FileWriter writer = new FileWriter(file, false);
//            if (content.size() < 0) return;
//            for (int i = 0; i < strings.length; i++) {
//                writer.write(strings[i]);
//                if (i < strings.length) writer.write("\r\n");
//            }
//            for (int i = content.size() - 1; i >= 0; i--) {
//                Log.d("writeList: ", i + "");
//
//                if (content.get(i).Isedit) {
//                    writer.write(content.get(i).Id + "," + content.get(i).DepositPlaceId);
//                } else {
//                    writer.write(content.get(i).Id);
//                }
//                writer.write("\r\n");
//            }
//            writer.close();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
//
//    }


    public static String readString(String file, String charset) {
        byte[] data = readBytes(file);
        String ret = null;

        try {
            ret = new String(data, charset);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ret;
    }

    public static String createName(String path) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String monthstr = month < 10 ? "0" + month : month + "";
        String daystr = day < 10 ? "0" + day : day + "";
        return path + "/" + year + monthstr + daystr + ".txt";

    }


    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}